<template>
	<view style="height: 100%">
		<!-- pages/orderSubmit/orderSubmit.wxml -->
		<!-- 地址 -->
		<view @tap="selectAddress" class="express v-center between">
			<view class="address">
				<view v-if="addressInfo.Fid">
					<view class="addname">
						{{ addressInfo.Fprovince }}{{ addressInfo.Fcity }}{{ addressInfo.Fzone }}{{ addressInfo.Faddress }}
					</view>
					<view class="adduname">{{ addressInfo.Fcontacter }} {{ addressInfo.Fmobile }}</view>
				</view>
				<view v-else>添加收货地址</view>
			</view>
			<view class="v-center end">
				<image class="right-icon" src="/static/file/images/new_right.png"></image>
			</view>
		</view>

		<!-- 订单列表 -->
		<view class="orderList">
			<view class="orderItem">
				<view>
					<view class="proList">
						<view class="proItem v-center around" v-for="(pro, i) in proList" :key="i">
							<image class="proImg" :src="pro.Fimg"></image>

							<view class="proContext">
								<view class="proName">{{ pro.Fsummary }}</view>
								<view class="proSku">{{ pro.Fattr }}</view>
							</view>

							<view class="proNum">
								<view class="proPrice">￥{{ pro.Fsaleprice }}</view>
								x{{ pro.Fnum }}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 订单留言 -->

		<!-- 优惠券 -->
		<view v-if="Fcouponnum > 0" class="couponContent">
			<view @tap="sqlCoupon" class="titleBar v-center between">
				<view>
					<text class="icon">惠</text>
					优惠券
				</view>
				<view class="v-center end">
					<text>{{ Fcouponnum }}张</text>
					<image class="right-icon" src="/static/file/images/new_right.png"></image>
				</view>
			</view>

			<view v-if="disPrice > 0" class="discounts v-center between">
				<view></view>
				<text class="v-center end">
					节省：￥{{ disPrice }}
					<image @tap="close" class="right-icon" src="/static/file/images/close.png"></image>
				</text>
			</view>
		</view>
		<view class="couponContent discounts v-center between">
			<input type="text" @input="leaveMsg" placeholder="您的留言" placeholder-class="phcolor" :value="message" />
		</view>
		<!-- 支付方式 -->

		<view class="bottomBar algin">
			<view class="left-half v-center">
				<text>
					<text class="num">共{{ totalNum }}件</text>
					实付:
				</text>
				<text class="totalPrice">￥{{ totalPrice - disPrice - Fpaymoney }}</text>
			</view>
			<view @tap="confirmOrder" class="right-half algin" :style="'background:' + color">确认支付</view>
		</view>
	</view>
</template>

<script>
	import floatIcon from '@/component/floatIcon/floatIcon';
	// pages/orderSubmit/orderSubmit.js
	var util = require('../../utils/util.js');
	var api = require('../../utils/prom.js');
	const app = getApp();
	export default {
		components: {
			floatIcon
		},
		data() {
			return {
				color: app.globalData.color,
				coupon: null,
				proList: [],

				//商品列表
				totalNum: 0,

				//总数量
				totalPrice: 0,

				//总价
				disPrice: 0,

				//充值券抵扣
				addressInfo: {
					Fid: '',
					Fprovince: '',
					Fcity: '',
					Fzone: '',
					Faddress: '',
					Fcontacter: '',
					Fmobile: ''
				},

				//收货地址
				message: '',

				//留言
				payData: {},

				//支付参数
				detail: {},

				//详情页带过来的信息
				Fbalance: 0,

				Fcouponnum: 0,
				Fpaymoney: 0,
				Fsource: 0,
				Fcode: '',
				Fids: '',
				payment: '',
				i: '',

				pro: {
					Fimg: '',
					Fsummary: '',
					Fattr: '',
					Fsaleprice: ''
				}
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			var that = this;
			var proList = [];
			that.Fsource = options.source;
			if (!util.isEmpty(options.Fcode)) {
				that.Fcode = options.Fcode;
			}
			//直接购买
			if (options.source == 1 || options.source == 4 || options.source == 5 || options.source == 6) {
				var row = JSON.parse(decodeURIComponent(options.Fsku));
				var Arr = JSON.parse(row.Fattr);
				var Fattr = '';
				for (var i in Arr) {
					Fattr += Arr[i].Fattr + '【' + Arr[i].Fvalue + '】';
				}
				row.Fattr = Fattr;
				row.Fnum = options.Fnum;
				row.Fscid = 0; //优惠券
				if (options.source == 5 || options.source == 4 || options.source == 6) {
					row.Faid = row.Fid;
				} //活动ID
				proList.push(row);
				//总数量 总价 
				that.proList = proList;
				that.totalNum = options.Fnum;
				that.totalPrice = (options.Fnum * row.Fsaleprice).toFixed(2);
				that.searchCoupon();
			}
			//购物车
			else if (options.source == 2) {
				if (options.ids) {
					var ids = options.ids;
					that.Fids = options.ids;
					that.searchProList(ids);
				}
			}
			//从预付款进入
			else if (options.source == 3) {
				var Fcode = options.Fcode;
				that.searchProListByCode(Fcode);
			}
			if (options.source != 3) {
				that.addressInfo = uni.getStorageSync('addressInfo');
				//查询默认地址
				if (util.isEmpty(uni.getStorageSync('addressInfo'))) {
					that.defaultAddress();
				}
			}
			var user = uni.getStorageSync('user');

		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			this.onShowClone3389();
		},
		//返回首页
		onHide: function() {
			uni.removeStorageSync('addressInfo');
			uni.removeStorageSync('shopInfo');
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			this.onShowClone3389({});
			uni.stopPullDownRefresh();
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function(res) {
			return {
				title: app.globalData.shareinfo.title,
				imageUrl: app.globalData.shareinfo.img,
				desc: app.globalData.shareinfo.desc,
				path: 'pages/index/index?Fid=' + uni.getStorageSync('uid')
				//点击分享的图片进到哪一个页面
			};
		},
		methods: {
			/**
			 * 生命周期函数--监听页面显示
			 */
			onShowClone3389: function() {
				var that = this;
			},

			doSetAddress: function(address) {
				console.log(address);
				this.addressInfo = address;
			},

			//查询优惠劵数量
			searchCoupon: function() {
				var that = this;
				var arr = [];
				that.proList.map(function(item) {
					arr.push({
						Fpid: item.Fspuid,
						Fnum: item.Fnum,
						Fprice: item.Fsaleprice
					});
				});
				api.request('SpCouponUser?token=' + uni.getStorageSync('token'), 'post', arr, function(res) {
					that.Fcouponnum = res.data.Rows.length;
				});
			},

			//通过订单号查询
			searchProListByCode: function(code) {
				var that = this;
				var param = {
					token: uni.getStorageSync('token'),
					Fcode: code
				};
				api.request('Sale', 'get', param, function(res) {
					var proList = [];
					var addressInfo = {
						Faddress: res.data.Head.Faddress,
						Fprovince: res.data.Head.Fprovince,
						Fcity: res.data.Head.Fcity,
						Fzone: res.data.Head.Fzone,
						Fcontacter: res.data.Head.Fname,
						Fmobile: res.data.Head.Fmobile
					};
					that.checkAddress(addressInfo);
					var totalNum = 0;
					for (var i in res.data.Detail) {
						totalNum += Number(res.data.Detail[i].Fnum);
					}
					res.data.Detail.map(function(item) {
						if (!util.isEmpty(item.Fattr)) {
							var Arr = JSON.parse(item.Fattr);
							var Fattr = '';
							for (var i in Arr) {
								Fattr += Arr[i].Fattr + '【' + Arr[i].Fvalue + '】';
							}
							item.Fattr = Fattr;
						}
						item.Fsaleprice = item.Fprice;
					});
					that.proList = res.data.Detail;
					that.totalPrice = Number(res.data.Head.Ftotalmoney).toFixed(2);
					that.totalNum = totalNum.toFixed(0);
					that.message = res.data.Head.Fsummary;
					that.searchCoupon();
				});
			},

			//从购物车查询订单
			searchProList: function(ids) {
				var that = this;
				var param = {
					token: uni.getStorageSync('token'),
					ids: ids
				};
				api.request('SpShopCart', 'get', param, function(res) {
					var proList = [];
					var totalNum = 0;
					var totalPrice = 0;
					res.data.Row.map(function(item) {
						totalNum += Number(item.Fnum);
						if (item.Faid == 0) {
							var Arr = JSON.parse(item.Sku.Fattr);
							var Fattr = '';
							for (var i in Arr) {
								Fattr += Arr[i].Fattr + '【' + Arr[i].Fvalue + '】';
							}
							item.Sku.Fattr = Fattr;
							item.Sku.Fscid = item.Fid;
							item.Sku.Faid = 0;
							item.Sku.Fnum = item.Fnum;
							proList.push(item.Sku);
							totalPrice += parseFloat(item.Sku.Fsaleprice) * parseFloat(item.Fnum);
						} else {
							var Arr = JSON.parse(item.Sku.Time);
							var Fattr = '';
							for (var i in Arr) {
								Fattr += Arr[i].Fattr + '【' + Arr[i].Fvalue + '】';
							}
							item.Time.Fattr = Fattr;
							item.Time.Fnum = item.Fnum;
							item.Time.Fscid = item.Fid;
							item.Time.Faid = item.Faid;
							proList.push(item.Time);
							totalPrice += parseFloat(item.Time.Fsaleprice) * parseFloat(item.Fnum);
						}
					});
					that.proList = proList;
					that.totalNum = totalNum.toFixed(0);
					that.totalPrice = totalPrice.toFixed(2);
					that.searchCoupon();
				});
			},

			checkAddress: function(address) {
				var that = this;
				var param = {
					token: uni.getStorageSync('token')
				};
				api.requestAll('SpAddress', 'get', param, function(res) {
					for (let i in res.data) {
						let row = res.data[i];
						if (
							row.Fprovince == address.Fprovince &&
							row.Fcity == address.Fcity &&
							row.Fzone == address.Fzone &&
							row.Fcontacter == address.Fcontacter &&
							row.Fmobile == address.Fmobile
						) {
							that.addressInfo = row;
							break;
						}
					}
				});
			},

			//查询默认地址
			defaultAddress: function() {
				var that = this;
				var param = {
					token: uni.getStorageSync('token'),
					Fisdefault: 1
				};
				api.request('SpAddress', 'get', param, function(res) {
					that.addressInfo = res.data.Address;
					uni.SetStorageSync('addressInfo', res.data.Address);
				});
			},

			//选择地址
			selectAddress: function() {
				uni.navigateTo({
					url: '/pages/myAddress/myAddress?back=1'
				});
			},

			//留言
			leaveMsg: function(e) {
				var that = this;
				var val = e.detail.value;
				that.message = val;
			},

			sqlCoupon: function(e) {
				uni.navigateTo({
					url: '/pages/coupon/coupon?proList=' + escape(JSON.stringify(this.proList))
				});
			},

			setCoupon: function(data) {
				this.coupon = data;
				this.disPrice = data.Ffacevalue;
			},

			closeCoupon: function() {
				this.coupon = {};
				this.disPrice = 0;
			},

			//提交预订单
			confirmOrder: function() {
				var that = this;
				if (null == that.addressInfo || util.isEmpty(that.addressInfo.Fid)) {
					uni.showToast({
						icon: 'none',
						title: '请选择收获地址！'
					});
					return;
				}
				//直接购买
				if (that.Fsource == 1)
					api.request('SpActivity?token=' + uni.getStorageSync('token'), 'get', {}, function(res) {
						that.buyNow(res.data.Message);
					});
				//购物车
				else if (that.Fsource == 2)
					api.request('SpActivity?token=' + uni.getStorageSync('token'), 'get', {}, function(res) {
						that.buyNow(res.data.Message);
					});
				//订单
				else if (that.Fsource == 3) {
					api.request('SpActivity?token=' + uni.getStorageSync('token'), 'get', {}, function(res) {
						that.buyNow(res.data.Message);
					});
				}
				//拼团开团
				else if (that.Fsource == 4)
					api.request('SpActivity?token=' + uni.getStorageSync('token'), 'get', {}, function(res) {
						that.openGroup(res.data.Message);
					});
				//团购
				else if (that.Fsource == 5)
					api.request('SpActivity?token=' + uni.getStorageSync('token'), 'get', {}, function(res) {
						that.joinGroup(res.data.Message);
					});
				//参团
				else if (that.Fsource == 6) {
					api.request('SpActivity?token=' + uni.getStorageSync('token'), 'get', {}, function(res) {
						that.joinUserGroup(res.data.Message);
					});
				}
			},

			//立即购买
			buyNow: function(Fsign) {
				var that = this;
				var Paylist = [];
				if (!util.isEmpty(that.coupon)) {
					Paylist.push({
						Ftcid: that.coupon.Fid,
						Fmoney: that.coupon.Ffacevalue,
						Ftype: 4
					});
				}
				if (!util.isEmpty(that.Fpaymoney)) {
					Paylist.push({
						Fmoney: that.Fpaymoney,
						Ftype: 2
					});
				}
				var rows = [];
				for (var i in that.proList) {
					var row = that.proList[i];
					rows.push({
						Faid: util.isEmpty(row.Faid) ? 0 : row.Faid,
						Fpid: row.Fspuid,
						Ftid: row.Fskuid,
						Fnum: row.Fnum,
						Fprice: row.Fsaleprice
					});
				}
				var param = {
					Fzid: 0,
					Faddressid: util.isEmpty(that.addressInfo.Fid) ? '' : that.addressInfo.Fid,
					Fcode: that.Fcode,
					Fsign: Fsign,
					Fids: that.Fids,
					Fmemo: that.message,
					Rows: rows,
					Paylist: Paylist
				};
				var lastMoney = that.totalPrice - that.disPrice - that.Fpaymoney;
				api.request('SpPrePay?token=' + uni.getStorageSync('token'), 'post', param, function(res) {
					var Fcode = res.data.Message;
					if (!util.isEmpty(that.Fcode)) {
						Fcode = that.Fcode;
					}
					if (lastMoney == 0) {
						// 余额支付
						that.cardPay(Fcode);
					} else {
						that.wxPayment(Fcode);
					}
				});
			},

			//开团
			openGroup: function(Fsign) {
				var that = this;
				var that = this;
				var Paylist = [];
				if (!util.isEmpty(that.coupon)) {
					Paylist.push({
						Ftcid: that.coupon.Fid,
						Fmoney: that.coupon.Ffacevalue,
						Ftype: 4
					});
				}
				if (!util.isEmpty(that.Fpaymoney)) {
					Paylist.push({
						Fmoney: that.Fpaymoney,
						Ftype: 2
					});
				}
				var row = that.proList[0];
				var param = {
					Fzid: 0,
					Faddressid: util.isEmpty(that.addressInfo.Fid) ? '' : that.addressInfo.Fid,
					Faid: row.Faid,
					Fsmlaid: 0,
					Fsign: Fsign,
					Fnum: row.Fnum,
					Fmemo: that.message,
					Paylist: Paylist
				};
				var lastMoney = that.totalPrice - that.disPrice - that.Fpaymoney;
				api.request('SpGroupUserOpen?token=' + uni.getStorageSync('token'), 'post', param, function(res) {
					var Fcode = res.data.Message;
					if (!util.isEmpty(that.Fcode)) {
						Fcode = that.Fcode;
					}
					if (lastMoney == 0) {
						// 余额支付
						that.cardPay(Fcode);
					} else {
						that.wxPayment(Fcode);
					}
				});
			},

			//参加拼团
			joinGroup: function(Fsign) {
				var that = this;
				var Paylist = [];
				if (!util.isEmpty(that.coupon)) {
					Paylist.push({
						Ftcid: that.coupon.Fid,
						Fmoney: that.coupon.Ffacevalue,
						Ftype: 4
					});
				}
				if (!util.isEmpty(that.Fpaymoney)) {
					Paylist.push({
						Fmoney: that.Fpaymoney,
						Ftype: 2
					});
				}
				var row = that.proList[0];
				var param = {
					Fzid: 0,
					Faddressid: util.isEmpty(that.addressInfo.Fid) ? '' : that.addressInfo.Fid,
					Faid: row.Faid,
					Fsmlaid: 0,
					Fsign: Fsign,
					Fnum: row.Fnum,
					Fmemo: that.message,
					Paylist: Paylist
				};
				var lastMoney = that.totalPrice - that.disPrice - that.Fpaymoney;
				api.request('SpGroupJoin?token=' + uni.getStorageSync('token'), 'post', param, function(res) {
					var Fcode = res.data.Message;
					if (!util.isEmpty(that.Fcode)) {
						Fcode = that.Fcode;
					}
					if (lastMoney == 0) {
						// 余额支付
						that.cardPay(Fcode);
					} else {
						that.wxPayment(Fcode);
					}
				});
			},

			//会员卡支付
			cardPay: function(Fcode) {
				var that = this;
				var param = {
					ForderNo: Fcode,
					Fmoney: that.Fpaymoney
				};
				api.request('SpSaleConfig?token=' + uni.getStorageSync('token'), 'post', param, function(res) {
					//  拼团
					if (that.Fsource == 6 || that.Fsource == 4) {
						uni.redirectTo({
							url: '/pages/pinDetail/pinDetail?Fcode=' + Fcode
						});
					}
					//团购
					else if (that.Fsource == 5) {
						var Ftotal = that.totalPrice;
						var address = escape(JSON.stringify(that.addressInfo));
						uni.redirectTo({
							url: '/pages/payResult/payResult?source=5&Fstatus=1&Ftotal=' + Ftotal +
								'&Fcode=' + Fcode + '&FaddressInfo=' + address
						});
					} else {
						var Ftotal = that.totalPrice;
						var address = escape(JSON.stringify(that.addressInfo));
						//普通购买
						uni.redirectTo({
							url: '/pages/payResult/payResult?source=1&Fstatus=1&Ftotal=' + Ftotal +
								'&Fcode=' + Fcode + '&FaddressInfo=' + address
						});
					}
				});
			},

			// //微信支付方式
			// wxPayment: function(code) {
			// 	var that = this;
			// 	that.Fcode = code;
			// 	var lastMoney = that.totalPrice - that.disPrice - that.Fpaymoney;
			// 	if (lastMoney <= 0) {
			// 		that.cardPay();
			// 		return;
			// 	}
			// 	var param = {
			// 		token: uni.getStorageSync('token'),
			// 		code: code
			// 	};
			// 	api.request('MiniPay', 'Get', param, function(res) {
			// 		console.log('支付回调');
			// 		that.wxPay(JSON.parse(res.data.Message));
			// 	});
			// },

			// //微信支付
			// wxPay: function(payData) {
			// 	var that = this;
			// 	uni.requestPayment({
			// 		timeStamp: payData.timeStamp,
			// 		nonceStr: payData.nonceStr,
			// 		package: payData.package,
			// 		signType: payData.signType,
			// 		paySign: payData.paySign,
			// 		success: function(res) {
			// 			console.log(res);
			// 			uni.showToast({
			// 				title: '支付成功',
			// 				success: function() {
			// 					//  拼团
			// 					if (that.Fsource == 6 || that.Fsource == 4) {
			// 						var row = that.proList[0];
			// 						uni.redirectTo({
			// 							url: '/pages/pinDetail/pinDetail?Fcode=' + that
			// 								.Fcode
			// 						});
			// 					}
			// 					//团购
			// 					else if (that.Fsource == 5) {
			// 						var Ftotal = that.totalPrice;
			// 						var Fcode = that.Fcode;
			// 						var address = escape(JSON.stringify(that.addressInfo));
			// 						uni.redirectTo({
			// 							url: '/pages/payResult/payResult?source=5&Fstatus=1&Ftotal=' +
			// 								Ftotal + '&Fcode=' + Fcode +
			// 								'&FaddressInfo=' + address
			// 						});
			// 					} else {
			// 						var Ftotal = that.totalPrice;
			// 						var Fcode = that.Fcode;
			// 						var address = escape(JSON.stringify(that.addressInfo));
			// 						//普通购买
			// 						uni.redirectTo({
			// 							url: '/pages/payResult/payResult?source=1&Fstatus=1&Ftotal=' +
			// 								Ftotal + '&Fcode=' + Fcode +
			// 								'&FaddressInfo=' + address
			// 						});
			// 					}
			// 				}
			// 			});
			// 		},
			// 		fail: function(res) {
			// 			console.log(res);
			// 			uni.showToast({
			// 				icon: 'none',
			// 				title: '支付失败'
			// 			});
			// 		},
			// 		complete: function(res) {
			// 			console.log(res);
			// 		}
			// 	});
			// }
 
	};
</script>
<style>
	@import './orderSubmit.css';
</style>